package com.wang.controller.admin;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.wang.controller.utils.JWTUtil;
import com.wang.entity.User;
import com.wang.service.UserService;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Classname AdminUserController
 * @Description TODO
 * @Date 2024/9/25 14:58
 * @Created by pc
 */
@RestController
@Slf4j
@CrossOrigin  // 拦截器 跨域注解会失效  过滤器是在拦截器之前执行的
@RequestMapping("/admin/user")
@Api(value = "后台用户接口",tags = "AdminUserController")
public class AdminUserController {

    @Resource
    private UserService userService;

    // 后台登录
    @PostMapping("/login")
    @ApiOperation(value = "后台登录",notes = "后台管理员登录")
    @ApiImplicitParam(name="user",value = "用户实体类",required = true,dataType = "User")
    public Map<String, Object> login(@RequestBody User user){
        // return userService.adminLogin(user);
        ResponseUtil r = userService.adminLogin(user);
        Map<String, Object> map = new HashMap<>();
        if(r.getCode().intValue() == 1012){
            // 登录成功
            User loginUser = (User) r.getData();
            // 携带用户数据  用户名 用户id 用户角色
            Map<String,String> payload = new HashMap<>();
            payload.put("username",loginUser.getUsername());
            payload.put("userId",loginUser.getId().toString());
            payload.put("roleId",loginUser.getRole().toString());
            // 生成JWT
            String token = JWTUtil.getToken(payload);
            map.put("status",true);
            map.put("msg","登录成功");
            map.put("token",token);

        }else {
            // 登录失败
            map.put("status",false);
            map.put("msg",r.getMsg());
        }
        return map;

    }

    // 测试token
    @GetMapping("/test")
    public Map<String,Object> test(String token){
        Map<String,Object> map = new HashMap<>();
        try {
            DecodedJWT verify = JWTUtil.verify(token);
            map.put("status",true);
            map.put("msg","请求成功");
            return map;
        } catch (SignatureVerificationException e) {
            e.printStackTrace();
            map.put("msg", "无效签名!");
        } catch (TokenExpiredException e) {
            e.printStackTrace();
            map.put("msg", "token过期!");
        } catch (AlgorithmMismatchException e) {
            e.printStackTrace();
            map.put("msg", "token算法不一致!");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "token无效");
        }
        map.put("status", false);
        return map;
    }


    // 测试token
    @GetMapping("/test2")
    public Map<String,Object> test2(){
        Map<String,Object> map = new HashMap<>();
        map.put("status",true);
        map.put("msg","请求成功");
        return map;
    }



    // 查询所有用户
    @GetMapping(value = "/findAll")
    @ApiOperation(value = "获取用户信息",notes = "后台查询所有用户信息")
    public ResponseUtil findAll(){
        ResponseUtil all = userService.findAll();
        return all;
    }

    //分页查询用户
    @PostMapping("/find/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页获取用户信息",notes = "后台分页查询所有用户信息")
    public ResponseUtil find(@PathVariable("pageNum")Integer pageNum,
                             @PathVariable("pageSize")Integer pageSize){
        return userService.find(pageNum,pageSize);
    }


    // 根据id 查询用户
    @GetMapping("/findById/{id}")
    @ApiOperation(value = "根据id查询用户",notes = "后台根据用户id查询用户详细信息")
    @ApiImplicitParam(name="id",value = "用户id",required = true,paramType = "path")
    public ResponseUtil findById(@PathVariable("id") Integer id){
        return userService.findById(id);
    }
}
